(function($) {

    $.fn.magefister4jPaging = function(options) {
        var opts = $.extend({},$.fn.magefister4jPaging.defaults,options);

        var original = this;

        var action = {

            init : function() {
                var totalPage = Math.ceil(opts.totalSize/opts.pageSize);
                var totalPageList = Math.ceil(totalPage/opts.pageListSize);
                var pageList = Math.ceil(opts.pageNo/opts.pageListSize);
                if (pageList < 1) pageList = 1;
                if (pageList > totalPageList) pageList = totalPageList;
                var startPageList = (pageList - 1) * opts.pageListSize + 1;
                var endPageList = startPageList + opts.pageListSize - 1;	

                if (startPageList < 1) startPageList = 1;
                if (endPageList > totalPage) endPageList = totalPage;
                if (endPageList < 1) endPageList = 1;
                
                //var innerHTML = action.getPageItemLink(1, '[First]', (opts.pageNo > 1), '');
                var innerHTML = "<table align=\"center\"><tr>";
                innerHTML += action.getPageItemLink((startPageList - 1), 'PRE', (startPageList > 1), 'pre_01');
                for (var i = startPageList; i <= endPageList; i++) {
                    innerHTML += action.getNumberLink(i, null, (opts.pageNo != i), ((opts.pageNo == i)? 'strong': ''));
                    if (i < endPageList) {
                        innerHTML += ' ';       // separate action
                    }
                }
                innerHTML += action.getPageItemLink((endPageList + 1),'NEXT',(endPageList < totalPage), 'next_01');
                innerHTML += "</tr></table>";
                //innerHTML += action.getPageItemLink(totalPage, '[End]', (opts.pageNo < totalPage), 'arrow next');
                $(original).html(innerHTML);
                
                //alert("[innerHTML]"+innerHTML );
            },

            getNumberLink : function(pageNo, text, useLink, className) {
            	
                if (useLink) {
                    return '<td><a href="javascript:'+opts.pageClickFunctionName+'(' + pageNo + ')">' + ((text != null && text != '')? text: pageNo) + '</a></td>\n';
                }
                else {
                	return '<td><a class="choice" href="javascript:'+opts.pageClickFunctionName+'(' + pageNo + ')">' + ((text != null && text != '')? text: pageNo) + '</a></td>\n';
                    /*return '<strong class="choice">' +
                            ((text != null && text != '')? text: pageNo) +
                            '</strong>';*/
                }
            },

            getPageItemLink : function(pageNo, text, useLink, className) {
                if(useLink) {
                    return '<td class=\''+className+'\'><a' + ((className != null && className != '')? ' class="' + className + '"': '') +
                            ' href="javascript:'+opts.pageClickFunctionName+'(' + pageNo + ')">' +
                            text +
                            '</a></td>\n';
                }
                else {
                    if(opts.showUnlinkedSymbols) {
                        return '<td><span>' +
                                    ((text != null && text != '')? text: pageNo) +
                                '</span></td>\n';
                    } else {
                        return '\n';
                    }
                }
            }

        };
        action.init();
    };

    $.fn.magefister4jPaging.defaults = {
        totalSize : 0,      // total size
        pageNo : 1,         // current page No
        pageSize : 10,     // list per page count
        pageListSize : 10,  // page bar count 1 2 3 4 5
        pageClickFunctionName : 'page_click',
        showUnlinkedSymbols : true
    };

})(jQuery);